package cn.xiaomubiao.oschine.viewpagerfragment.tweetspagerfragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xiaomubiao.oschine.MyApplication;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.activity.MyInfoBaseActivity;
import cn.xiaomubiao.oschine.adapter.tweets_pager_adapter.TweetsBaseRecyclerAdapter;
import cn.xiaomubiao.oschine.base.BaseEachFragment;
import cn.xiaomubiao.oschine.bean.Tweet;
import cn.xiaomubiao.oschine.bean.TweetsList;
import cn.xiaomubiao.oschine.contants.Host;
import cn.xiaomubiao.oschine.util.HttpUtil;


/**
 * Created by hasee on 2017/4/6.
 */

public abstract class TweetsBaseFragment extends BaseEachFragment {
    @BindView(R.id.recyler_pager)
    XRecyclerView mRecylerPager;
    /*@BindView(R.id.refresh_pager)
    SwipeRefreshLayout mRefresh;*/
    private View mView;
    private TweetsList mTweetsList;
    private List<Tweet> mTweets;
    private int loadingNum;
    private TweetsBaseRecyclerAdapter mTweetsXRecylerAadapter;


    public abstract String getUrl();

    @Override
    public View loadSuccess() {
        mView = View.inflate(getActivity(), R.layout.view_tweets_pager_layout, null);
        ButterKnife.bind(this, mView);
        initRecylerView();
        return mView;

    }

    /**
     * 初始化RecylerView
     */
    private void initRecylerView() {
        mTweetsXRecylerAadapter = new TweetsBaseRecyclerAdapter(mTweets);
        mTweetsXRecylerAadapter.setTweetsOnItemClickListener(new TweetsBaseRecyclerAdapter.MyOnClickListener() {
            @Override
            public void onClick( View v, int position ) {
                String url = Host.HOST_DEFAULT+"detail/tweet_detail/"+mTweets.get(position).getId()+".xml";
                Toast.makeText(getContext(),url, Toast.LENGTH_SHORT).show();
                /*String say = "ttp://192.168.1.100:8080/oschina/commentlist/catalog3id"+mTweets.get(position).getId()+"page"+loadingNum+".xml";
                Toast.makeText(getContext(), say, Toast.LENGTH_SHORT).show();*/
                Intent intent = new Intent(getActivity(), MyInfoBaseActivity.class);
                intent.putExtra("id",mTweets.get(position).getId());
                intent.putExtra("title","动弹详情");
                intent.putExtra("className",TweetsDetailFragment.class.getName());
                startActivity(intent);
            }
        });

        mRecylerPager.setAdapter(mTweetsXRecylerAadapter);
        mRecylerPager.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecylerPager.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(getContext(), "刷新", Toast.LENGTH_SHORT).show();
                MyApplication.mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mRecylerPager.refreshComplete();
                    }
                },2000);
            }

            @Override
            public void onLoadMore() {
                ++loadingNum;
                if(loadingNum*mTweetsList.getPagesize() >= mTweetsList.getTweetCount()) {
                    mRecylerPager.loadMoreComplete();
                    mRecylerPager.setNoMore(true);
                    return;
                }
                final String loadingValue = "page" + loadingNum + ".xml";
                HttpUtil<TweetsList> httpUtil = new HttpUtil<>(getContext(), TweetsList.class);
                httpUtil.getStringData(getUrl() + loadingValue, new HttpUtil.OnGetDataListener<TweetsList>() {
                    @Override
                    public void onSuccess( TweetsList tweetsList ) {
                        if(tweetsList  == null) {
                            loadMoreCompleteAndNotifyData();
                            return;
                        }
                        List<Tweet> tweetslist = tweetsList.getTweetslist();
                        mTweets.addAll(tweetslist);
                        loadMoreCompleteAndNotifyData();
                    }

                    @Override
                    public void onFailure( Exception e ) {
                        mCommonPager.showFailPager();
                    }
                });

            }
        });
    }

    private void loadMoreCompleteAndNotifyData() {
        MyApplication.mHandler.post(new Runnable() {
            @Override
            public void run() {
                mRecylerPager.loadMoreComplete();

                mTweetsXRecylerAadapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void loadData() {

        loadingNum = 0;
        HttpUtil<TweetsList> httpUtil = new HttpUtil<>(getContext(), TweetsList.class);
        httpUtil.getStringData(getUrl() + "page0.xml", new HttpUtil.OnGetDataListener<TweetsList>() {
            @Override
            public void onSuccess( TweetsList tweetsList ) {
                mTweetsList = tweetsList;
                mTweets = mTweetsList.getTweetslist();
                mCommonPager.showSuccessPager(tweetsList);
            }
            @Override
            public void onFailure( Exception e ) {
//                Toast.makeText(getContext(), "获取失败", Toast.LENGTH_SHORT).show();
                mCommonPager.showFailPager();
            }
        });
    }
}
